package org.idea.iedis.framework.server.datastruct.stack;

import org.idea.iedis.framework.server.datastruct.string.Sds;

/**
 * @Author linhao
 * @Date created in 5:08 下午 2021/4/30
 */
public class Stack {

    Node head = null;
    Node tail = null;
    int size;

    public Stack() {
    }

    public Stack(Node head) {
        this.head = head;
        this.size = 1;
    }

    public void sPut(String val) {
        if (head == null) {
            head = new Node(new Sds(val), null, null);
            this.size = 1;
            return;
        }
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        Node newItem = new Node(new Sds(val), null, temp);
        temp.next = newItem;
        this.tail = newItem;
        this.size++;
    }

    public Sds sPop() {
        Node pop = tail;
        this.tail = tail.pre;
        return pop != null ? pop.getItem() : null;
    }

    public int getSize() {
        return size;
    }

}

class Node {
    Sds item;
    Node next;
    Node pre;

    public Node(Sds item, Node next, Node pre) {
        this.item = item;
        this.next = next;
        this.pre = pre;
    }

    public Sds getItem() {
        return item;
    }

    public void setItem(Sds item) {
        this.item = item;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Node getPre() {
        return pre;
    }

    public void setPre(Node pre) {
        this.pre = pre;
    }
}
